/*
* 全局通用表单正则验证
* */

//自定义验证

const validateUserName = (rule, value, callback) => { // 用户名验证
    let minAge = 6;
    let maxAge = 16;
    if (value.length < minAge || value.length > maxAge) {
        callback(new Error('用户名必须在' + minAge + '-' + maxAge + '位之间'));
    } else {
        callback();
    }
};
const validatezhong = (rule, value, callback) => {//中文验证
    let reg = /^[\u4e00-\u9fa5]/;
    if (value && !reg.test(value)) {
        callback(new Error('只能输入中文!'));
    } else {
        callback();
    }
};
const validateEmail = (rule, value, callback) => {//邮箱验证
    let reg = /^\w{5,}@[a-z0-9]{2,3}\.[a-z]+$|\,$/;
    if (value && !reg.test(value)) {
        callback(new Error('输入邮箱格式有误!'));
    } else {
        callback();
    }
};
const validatenetzhong = (rule, value, callback) => {//中文验证
    let reg = /[^\w\.\/]/ig;
    if (value && reg.test(value)) {
        callback(new Error('输入格式有误'));
    } else {
        callback();
    }
};
const validateUserPassword = (rule, value, callback) => { //密码正则验证
    let minAge = 6;
    let maxAge = 16;
    if (value.length < minAge || value.length > maxAge) {
        callback(new Error('密码必须在' + minAge + '-' + maxAge + '位之间'));
    } else {
        callback();
    }
};
const validateAge = (rule, value, callback) => { // 年龄正则验证
    let minAge = 0;
    let maxAge = 120;
    if (value < minAge || value > maxAge) {
        callback(new Error('年龄必须在' + minAge + '-' + maxAge + '岁之间'));
    } else {
        callback();
    }
};
const validateIdcard = (rule, value, callback) => { // 身份证正则验证
    let idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if (!idcardReg.test(value)) {
        callback(new Error('请输入合法的身份证号码'));
    } else {
        callback();
    }
};
const validatePhone = (rule, value, callback) => { // 手机校验
    let phoneReg = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/
    if (!phoneReg.test(value)) {
        callback(new Error('请输入合法的电话号码'));
    } else {
        callback();
    }
};
const validatePeople = (rule, value, callback) => { // 适用人数
    let peopleReg = /^(0|\+?[1-9]\d{0,9})$/;
    if (!peopleReg.test(value)) {
        callback(new Error('格式有误'));
    } else {
        callback();
    }
};
const validateNumber2 = (rule, value, callback) => {//整数和小数点2位（万元）
    let reg = /^\d{0,6}(\.\d{0,2})?$/g;
    if (value && !reg.test(value)) {
        callback(new Error('整数输入六位；有小数点的保留两位共计八位'));
    } else {
        callback();
    }
};
const validatePhone1 = (rule, value, callback) => { // 手机/座机校验(必填)
    let phoneReg = /^1[3456789]\d{9}$/;
    let phoneReg1 = /^(0\d{2,3}-)?\d{7,8}$/
    if (!phoneReg.test(value)) {
        if (!phoneReg1.test(value)) {
            callback(new Error('请输入合法的电话号码'))
        } else {
            callback()
        }
    } else {
        callback()
    }
};
const validatePrice = (rule, value, callback) => { // 一口价/^d+$/
    let priceReg = /^(([1-9][0-9]{0,9})|(([0]\.\d{1,2}|[1-9][0-9]{0,9}\.\d{1,2})))$/;
    if (!priceReg.test(value)) {
        callback(new Error('只能输入数字且数字最多保留两位小数'));
    } else {
        callback();
    }
};
const deptSort = (rule, value, callback) => { // 部门顺序
    let priceReg = /^[1-9]\d*$/;
    if (!priceReg.test(value)) {
        callback(new Error('部门顺序必须为数字'));
    } else {
        callback();
    }
};
const Number = (rule, value, callback) => { // 部门顺序
    let priceReg = /^[1-9]\d*$/;
    if (!priceReg.test(value)) {
        callback(new Error('救助金额必须为数字'));
    } else {
        callback();
    }
};

export default {
    required: { required: true, message: '该项不能为空', trigger: 'blur' }, // 必填项
    requireds: { required: true, message: '该项不能为空', trigger: 'change' }, // 必填项
    age: [ // 年龄验证规则
        { type: 'number', message: '年龄必须为数字值', trigger: 'blur' },
        { validator: validateAge, trigger: 'blur' },
    ],
    sort: [ // 部门验证规则
        { validator: deptSort, trigger: 'blur' },
    ],
    inputnumber: [ // 数字验证规则
        { validator: Number, trigger: 'blur' },
    ],
    idcard: [ // 身份证验证规则
        { validator: validateIdcard, trigger: 'blur', required: true, },
    ],//
    zhongWen: [ // 中文验证
        { validator: validatezhong, trigger: 'blur' },
    ],
    nozhongWen: [ // 非中文验证
        { validator: validatenetzhong, trigger: 'blur' },
    ],
    phone: [ // 手机校验
        { validator: validatePhone, trigger: 'blur', required: false, },
    ],
    phone1: [ // 手机/座机校验(必填)
        { validator: validatePhone1, trigger: 'blur', required: true, },
    ],
    price: [ // 一口价校验
        { validator: validatePrice, trigger: 'blur' },
    ],
    people: [ // 适用人数校验
        { validator: validatePeople, trigger: 'blur', },
    ],
    userName: [ // 用户名
        { validator: validateUserName, trigger: 'blur' },
    ],
    userPassword: [ // 密码
        { validator: validateUserPassword, trigger: 'blur' },
    ],
    email: [
        { validator: validateEmail, trigger: 'blur' }
    ]
}
